<?php
/**
 * Achievements' action functions.
 *
 * Action functions are exactly the same as screen functions, however they do not
 * have a template screen associated with them. Usually they will send the user
 * back to the default screen after execution.
 *
 * @author Paul Gibbs <paul@byotos.com>
 * @package Achievements for BuddyPress
 * @subpackage actions
 *
 * $Id$
 */

/**
 * Loads an Achievement's activity stream's RSS feed
 *
 * @global object $bp BuddyPress global settings
 * @since 2.1 (renamed from dpa_action_achievement_feed)
 */
function dpa_screen_achievement_feed() {
	global $bp;

	if ( $bp->current_component != $bp->achievements->slug || !$bp->loggedin_user->id || !$bp->is_single_item || DPA_SLUG_ACHIEVEMENT_ACTIVITY_RSS != $bp->current_action )
		return;

	$wp_query->is_404 = false;
	status_header( 200 );
	dpa_achievement_activity_filter();

	include_once( 'feeds/dpa-myachievements-feed.php' );
	die;
}
add_action( 'bp_setup_nav', 'dpa_screen_achievement_feed', 11 );

/**
 * Adds Achievements' XML-RPC methods.
 *
 * @param array $methods Existing XML-RPC methods
 * @return array
 * @since 2.1
 */
function dpa_add_xmlrpc_methods( $methods ) {
	$methods['achievements.getUserScore'] = 'dpa_xmlrpc_getUserScore';
	return $methods;
}
add_filter( 'xmlrpc_methods', 'dpa_add_xmlrpc_methods' );

/**
 * Retrieve user's achievement score.
 *
 * @global wp_xmlrpc_server $wp_xmlrpc_server
 * @param string $argv[0] The username for authentication
 * @param string $argv[1] The password for authentication
 * @return string|IXR_Error User's score. Returns an IXR_Error object on authentication failure.
 * @since 2.1
 */
function dpa_xmlrpc_getUserScore( $argv ) {
	global $wp_xmlrpc_server;

	$wp_xmlrpc_server->escape( $argv );
	list( $username, $password ) = $argv;

	if ( !$user = $wp_xmlrpc_server->login( $username, $password ) )
		return $wp_xmlrpc_server->error;

	do_action( 'xmlrpc_call', 'achievements.getUserScore', $argv );
	return dpa_get_member_achievements_score( $user->ID );
}

//TODO: Implement achievement CRUD XML-RPC methods once post type migration is complete.
?>